import { AnyAction } from "redux";
import produce from "immer";
import type { Menu } from "@/routers/types";

interface PermissionState {
	// Permission code list
	permCodeList: string[] | number[];
	// Whether the route has been dynamically added
	isDynamicAddedRoute: boolean;
	// To trigger a menu update
	lastBuildMenuTime: number;
	// Backstage menu list
	backMenuList: Menu[];
	frontMenuList: Menu[];
}

const permissionStore: PermissionState = {
	permCodeList: [],
	// Whether the route has been dynamically added
	isDynamicAddedRoute: false,
	// To trigger a menu update
	lastBuildMenuTime: 0,
	// Backstage menu list
	backMenuList: [],
	// menu List
	frontMenuList: []
};

const permission = (state: PermissionState = permissionStore, action: AnyAction) =>
	produce(state, draftState => {
		switch (action.type) {
			case "set_perm_code_list":
				draftState.permCodeList = action.payload;
				break;
			case "set_is_dynamic_added_route":
				draftState.isDynamicAddedRoute = action.payload;
				break;
			case "set_last_build_menu_time":
				draftState.lastBuildMenuTime = action.payload;
				break;
			case "set_back_menu_list":
				draftState.backMenuList = action.payload;
				break;
			case "set_front_menu_list":
				draftState.frontMenuList = action.payload;
				break;
			default:
				return draftState;
		}
	});

export default permission;
